<?php

/**
 * Description of DbSearch
 * @author Nick Swartelé
 */
require_once FILE_DB_DATABASE;

class DbSearch extends Database {

    private $type = '';
    private $term = '';
    private $tablename = '';
    private $columnname = '';
    private $query = '';

    public function __construct($type, $term) {
        parent::__construct();

        $this->setTerm($term);
        $this->setType($type);
    }

    public function DbSearch() {
        switch ($this->getType()) {

            case "Person":
                $this->setTablename('persoon');
                $this->setQuery("SELECT * FROM " . $this->getTablename() . " WHERE naam LIKE '%" . $this->getTerm() . "%'");      
                break;

            case "Bedrijfsonderdeel":
                $this->setTablename('bedrijfsonderdeel');
                $this->setQuery("SELECT * FROM " . $this->getTablename() . " WHERE beschrijving LIKE '%" . $this->getTerm() . "%'");               
                break;

            case "WPI":
                $this->setTablename('inspectie');
                $this->setQuery("SELECT * FROM " . $this->getTablename() . " WHERE project LIKE '%" . $this->getTerm() . "%'"); 
                break;
        }

        if ($this->dbquery($this->getQuery()) or die("SQL fout")) {

            // Controle of er iets gevonden is in de database
            if ($this->dbNumRows() >= 1) {
                return $this->dbFetchAll();
            } else {

//                 echo ("FAILED FetchAll from ".($this->tablename));

                return FALSE;
            }
        } else {
            throw new Exception(DB_ERROR_COULDNOTSELECT);
        }

        if (DEBUG_LVL & DEBUG_LVL_QUERY)
            echo __FILE__ . ' ' . __LINE__ . ': ' . $this->dbquery . '<br />';
    }

    //Getters
    public function getColumnname() {

        foreach ($this->columnname as $key => $array) {
            echo '<pre>';
            echo($array);
            echo '</pre>';
        }
    }

    public function getTerm() {
        return $this->term;
    }

    public function getType() {
        return $this->type;
    }

    public function getTablename() {
        return $this->tablename;
    }

    //Setters
    public function setTerm($term) {
        $this->term = $term;
    }

    public function setType($type) {
        $this->type = $type;
    }

    public function setTablename($tablename) {
        $this->tablename = $tablename;
    }

    public function setColumnname($columnname) {
        $this->columnname = $columnname;
    }
    public function getQuery() {
        return $this->query;
    }

    public function setQuery($query) {
        $this->query = $query;
    }



}

?>
